package com.pjb.springbootjjwt.store.utils;

import com.google.gson.Gson;
import com.pjb.springbootjjwt.store.Defines;
import com.pjb.springbootjjwt.store.entity.Error;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class FileUtil {
    public static final int DEFAULT_SIZE = 16;// 默认字段宽度
    public static final String DIR = "./data/";
    private static final Gson gson = new Gson();
    public static String USE_DB = "test";//默认数据库

    /**
     * 获取行长度
     *
     * @return
     */
    public static int getLineSize(String table) throws IOException {
        int fieldTotal = StringUtil.total(Defines.fieldsLength(table));
        int lineSize = fieldTotal + FileUtil.DEFAULT_SIZE + 1;
        return lineSize;
    }

    /**
     * 切换当前数据库
     *
     * @param space
     * @throws Exception
     */
    public static void use(String space) throws Exception {
        File file = new File(DIR + "/" + space);
        if (file.exists()) {
            USE_DB = space;
            System.out.println("当前使用数据库:" + USE_DB);
        } else {
            System.out.println("数据库:" + space + "不存在");
        }
    }

    /**
     * 获取表路径
     *
     * @param table
     * @return
     */
    public static String getPath(String table) {
        return USE_DB + "/" + table;
    }

    /**
     * 获取文件列表
     *
     * @param path
     * @return
     */
    public static Set<String> getDir(String path) throws Exception {
        if (StringUtils.isEmpty(path) && !USE_DB.contains("@")) throw new Exception(Error.E90007);
        File file = new File(DIR + "/" + path);
        if (file.exists() && file.isDirectory()) {
            File[] list = file.listFiles();
            Set<String> r = new HashSet<>();
            for (File f : list) {
                String n = f.getName();
                if (n.contains(".")) n = n.substring(0, n.indexOf("."));

                r.add(n);
            }
            return r;
        }
        return null;
    }

    public static void makeFolder(String path) throws Exception {
        File file = new File(DIR + "/" + path);
        if (file.getParentFile() == null || file.exists() == false) {
            boolean r = file.mkdirs();
            if (!r) throw new Exception("文件操作失败！path:" + path);
            System.out.println("操作成功!");
        } else {
            System.out.println("目录存在!");
        }
    }

    public static void removeFolder(String path) throws Exception {
        File file = new File(DIR + "/" + path);
        boolean r = file.delete();
        if (!r) throw new Exception("删除目录操作失败！path:" + path);
        System.out.println("删除目录操作成功!");
    }

    public static void removeFile(String table) throws Exception {
        File file = new File(DIR + getPath(table));
        boolean r = file.delete();
        if (!r) throw new Exception("drop table操作失败！table:" + table);
    }

    public static void removeFile(String space, String name) throws Exception {
        File file = new File(DIR + "/" + space + "/" + name);
        if (!file.exists()) throw new Exception("文件不存在！table:" + name);
        boolean r = file.delete();
        if (!r) throw new Exception("drop table操作失败！table:" + name);
    }

    public static void write(String name, String c) throws IOException {
        java.io.File file = new java.io.File(DIR + name);
        FileUtils.write(file, c, "utf-8");
    }

    public static void append(String name, String c) throws IOException {
        java.io.File file = new java.io.File(DIR + name);
        FileUtils.write(file, c, "utf-8", true);
    }

    public static String read(String table) throws IOException {
        java.io.File file = new java.io.File(DIR + "/" + FileUtil.getPath(table));
        String c = FileUtils.readFileToString(file, "utf-8");
        return c;
    }

    public static String readFile(String space, String name) throws IOException {
        java.io.File file = new java.io.File(DIR + "/" + space + "/" + name);
        String c = FileUtils.readFileToString(file, "utf-8");
        return c;
    }

    public static String read(String dir, String name) throws IOException {
        java.io.File file = new java.io.File(dir + name);
        String c = FileUtils.readFileToString(file, "utf-8");
        return c;
    }

    /**
     * 覆盖字符串
     *
     * @param data
     * @return
     */
    public static String overStr(List<String> data, List<Integer> len) {
        String c = "";
        for (int i = 0; i < data.size(); i++) {
            try {
                c += StringUtil.over(data.get(i), len.get(i));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return c;
    }

    /**
     * 覆盖字符串
     *
     * @param s
     * @return
     */
    public static String overStr(String s, int len) {
        try {
            return StringUtil.over(s, len);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 将覆盖字符串转为对象
     *
     * @param c
     * @return
     */
    public static List<String> reover(String c, List<Integer> len) {
        List<String> r = new ArrayList<>();
        int i = 0;
        while (c.length() > 0) {
            String s = c.substring(0, len.get(i));
            r.add(s.trim());
            c = c.substring(len.get(i));
            i++;
        }
        return r;
    }

    /**
     * 对象转字符串
     *
     * @param obj
     * @return
     */
    public static String jsonStr(Object obj) {
        return gson.toJson(obj);
    }

    /**
     * 字符串转对象
     *
     * @param jsonStr
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T toJson(String jsonStr, Class<T> clazz) {
        return gson.fromJson(jsonStr, clazz);
    }

    public static void main(String[] args) throws Exception {
        //removeFolder(dir + "/vv1");
//        makeFolder(DIR + "/vv1");
//        System.out.println("c:" + read("vv"));
        Set<String> list = FileUtil.getDir("test");
        System.out.println(list);
    }
}
